var calendar_year = 0;
var calendar_month = 0;
var calendar_day = 0;
//日历
$(document).ready(function(){
	var date = new Date();
	calendar_year = date.getFullYear();
	calendar_month = date.getMonth();
	calendar_day = date.getDate();
	showCalendar (calendar_year, calendar_month,calendar_day);
	markCal ();
});
//前一月
function decrease () {
	if (calendar_month == 0) {//月份减到1月的时候则跳到前一年的12月份
		calendar_year--;//年份减一年
		calendar_month = 11;//月份调到12月份
	} else {
		calendar_month = calendar_month - 1;
	}
	showCalendar (calendar_year, calendar_month,calendar_day);
	markCal ();
}
//后一月
function increase () {
	if (calendar_month == 11) {//月份加到12月的时候则跳到下一年的1月份
		calendar_year++;//年份增加一年
		calendar_month = 0;//月份调到1月份
	} else {
		calendar_month = calendar_month + 1;
	}
	showCalendar (calendar_year, calendar_month,calendar_day);
	markCal ();
}
//给有任务或有报送需求的日期天加上标识 
function markCal () {
	var days = getReceiveDay (calendar_year, calendar_month + 1);
	if (days != null && days != "") {
		for (var i = 0; i < days.length; i++) {
			$(".month-body li").each(function(){
				var a = $("span",this).text();
				var b = days[i];
				if (a != null && a != "" && a.length < 2) {
					a = "0" + a;
				}
				if (b != null && b != "" && b.length < 2) {
					b = "0" + b;
				}
				if (a != null && a != "" && b != null && b != "" && a == b) {
					$(this).addClass("blue");
				}
			});
		}
	}
}
//获取当前用户回款计划时间表
function getReceiveDay (nowYear, nowMonth) {
	var data = null;
	$.ajax({
		type:'post',
		url:'../repayment/getReceiveDay.htm',
		data: {"nowYear":nowYear,"nowMonth":nowMonth},
		dataType:'json',
		async: false,
		success:function(objs){
			data = objs;
		}
	});
	return data;
}
//绘制显示日历
function showCalendar (calendar_year, calendar_month,calendar_day) {
     var date = new Date(calendar_year, calendar_month,calendar_day);
	 //绘制月历体
     $(".month-body li").remove();
	 for(var i = 0;i < 42; i++){
	 	$("<li><span></span></li>").appendTo(".month-body").addClass("month-cell"); 
	 }
	 var add_month = calendar_month + 1;//获取月份
	 if(add_month < 10){
		 add_month = "0" + add_month; 
	 }
	 var week = date.getDay();//获取星期
	 var monthsNum = [31,28,31,30,31,30,31,31,30,31,30,31];//一年中每个月的最大日期号
	 var isleapyear = calendar_year%4;//判断闰年
	 if(isleapyear == 0){
		  monthsNum[1]=29;
	 }
	 //星期为0就代表是星期天,使用7代表星期天
     if(week == 0){
   	 	  week = 7;
	 }
	 var firstDay = week - (calendar_day%7 - 1);//此计算非常重要， 计算月初星期数
	 if(firstDay == 7){//如果月初为七，归零
		  firstDay = 0;
	 }
	 if(firstDay < 0){ //如果月初为负，加七循环
		  firstDay += 7;
	 }
	 var f = firstDay;
	 for(var j = 1;j <= monthsNum[calendar_month]; j++) {
		  $("li.month-cell span").eq(f).text(j).parent().addClass("pink");
		  f++; 
	 }
	 
	 //头部显示当前月份
	 $(".rili_title .t_c").html(calendar_year + "年" + add_month + "月" + "<a href=\"javascript:void(0)\" class=\"l\" onclick=\"decrease();\"><</a><a href=\"javascript:void(0)\" class=\"r\" onclick=\"increase();\">></a>");
	 
	 //点击日期
	 $("li.month-cell").click(function(){
		 var day = $.trim($("span",this).text());
		 if (day != null && day != "") {
			 var date = calendar_year + "-";
			 if ((calendar_month+1).toString().length < 2) {
				date = date + "0" + (calendar_month+1);
			 } else {
				date = date + (calendar_month+1);
			 }
			 date = date + "-";
			 if (day.length < 2) {
				 date = date + "0" + day;	
			 } else {
				 date = date + day;
			 }
			 showDataList (date);
		 }
	 });
}
//显示收款信息
function showDataList (d) {
	window.location.href="../investInfo/calInvestInfo.htm?arrivalTime="+d;
} 